package com.halfsword.creativecraft.block.custom.machine;

import com.halfsword.creativecraft.block.custom.machine.blockentity.SpaceShardCollectorMachineBlockEntity;
import com.halfsword.creativecraft.block.custom.machine.blockentity.TileEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

/**
 * 空间碎片收集器具体实现
 * 功能特点：
 * 1. 4输入槽，1个主输出槽，1个副输出槽
 * 2. 无合成顺序要求
 * 3. 维度敏感的矿石生成
 */
public class SpaceShardCollectorMachineBlock extends BaseBlockEntityBlock {

    public SpaceShardCollectorMachineBlock(BlockBehaviour.Properties properties) {
        super(properties);
    }

    // ==== 方块实体创建 ==== //
    @Override
    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        return new SpaceShardCollectorMachineBlockEntity(pos, state);
    }

    // ==== Ticker 实现 ==== //
    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(
            Level level, BlockState state, BlockEntityType<T> type) {

        if (level.isClientSide()) {
            return null;
        }

        return createTickerHelper(
                type,
                TileEntities.SPACE_SHARD_COLLECTOR_MACHINE_BE.get(),
                (lvl, pos, st, be) -> be.tick(lvl, pos, st)
        );
    }

    // ==== 重写特定行为 ==== //
    @Override
    protected void handleBlockEntityDrops(Level level, BlockPos pos, BlockEntity blockEntity) {
        if (blockEntity instanceof SpaceShardCollectorMachineBlockEntity machine) {
            machine.drops();
        }
    }

    @Override
    protected InteractionResult handleBlockEntityInteraction(ServerPlayer player, BlockPos pos, BlockEntity blockEntity) {
        if (blockEntity instanceof SpaceShardCollectorMachineBlockEntity machine) {
            NetworkHooks.openScreen(player, machine, pos);
            return InteractionResult.CONSUME;
        }
        return InteractionResult.FAIL;
    }
}